package com.ww.gss.util;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ww.gss.vo.param.GSSParamVO;
import com.ww.gss.vo.param.DBOpsParam;

public class GSSSystemUtils {
	private static Logger logger = LoggerFactory.getLogger(GSSSystemUtils.class);

	public static boolean isStrNull(String str) {
		if (str == null || "".equals(str)) {
			return true;
		}
		return false;
	}

	public static boolean isInWhiteList(String reqeusturl) {
		if (reqeusturl.indexOf("index") >= 0 || reqeusturl.indexOf("verifyCodeImage") >= 0
				|| reqeusturl.indexOf("authenticateUser") >= 0 || reqeusturl.indexOf(".js") >= 0
				|| reqeusturl.indexOf(".css") >= 0 || reqeusturl.indexOf(".json") >= 0|| reqeusturl.indexOf(".png") >= 0) {
			return true;
		}
		return false;
	}

	public static String decode(String s) {
		return StringUtils.newStringUtf8(Base64.decodeBase64(s));
	}

	public static String uRLDecoder(String s) {
		String result = "";
		try {
			result = URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public static String encode(String s) {
		return Base64.encodeBase64String(StringUtils.getBytesUtf8(s));
	}

	public List<Map<String, String>> buildParameterList(List<?> list) {
		List<Map<String, String>> resultlist = null;
		try {
			resultlist = new ArrayList<Map<String, String>>();
			Field[] fields = null;

			for (Object o : list) {
				logger.info("类：" + o.getClass().getName());
				fields = o.getClass().getDeclaredFields();
				Map<String, String> map = new HashMap<String, String>();
				for (Field field : fields) {

					field.setAccessible(true);
					String proName = field.getName();
					Object proValue = field.get(o);
					map.put(proName.toUpperCase(), String.valueOf(proValue));
					logger.info("key：" + proName + "-------value:" + proValue);
				}
				resultlist.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultlist;
	}

	public void isValidID(String idcode, String name) throws ParseException, IOException {
		// String apikey = tzconfigobj.getIdApiKey();
		// String apihost = tzconfigobj.getIdApiUrl();

		String apikey = "318fc3935e6ac0a2";
		String apihost = "http://api.jisuapi.com/idcard/query";

		String apiurl = apihost + "?appkey=" + apikey + "&idcard=" + idcode;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(apiurl);
		httpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		httpGet.addHeader("Connection", "Keep-Alive");
		httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
		httpGet.addHeader("Cookie", "");

		CloseableHttpResponse response = httpclient.execute(httpGet);
		int status = response.getStatusLine().getStatusCode();
		if (status >= 200 && status < 300) {
			String result = EntityUtils.toString(response.getEntity());
			logger.debug("result=>" + result);
		}

	}

	public static DBOpsParam buildDBParameter(String module, String action, Map parammap) {

		DBOpsParam param = new DBOpsParam();
		param.setModuleName(module);
		param.setActionName(action);
		param.setParamMap(parammap);
		return param;
	}

	public static String getToken1(String clientID) {
		PasswordHelper helper = new PasswordHelper();
		String currentdate = DateUtils.getCurrentDate();
		String result = helper.encryptString(clientID + ":" + currentdate);
		return result;

	}

	public static String generateToken2(String partyID) {
		PasswordHelper helper = new PasswordHelper();
		String result = helper.encryptString(partyID);
		return result;
	}

	public static boolean validateToken2(GSSParamVO vo) {
		String partyID = vo.getPartyID();
		String token2 = vo.getAuthorization2();
		PasswordHelper helper = new PasswordHelper();
		String result = helper.encryptString(partyID);
		if (GSSSystemUtils.isStrNull(token2) || !result.equals(token2)) {
			return false;
		}
		return true;
	}

	public static Map<String, String> buildParameterMap(GSSParamVO vo) {
		Map<String, String> paramap = new HashMap<String, String>();
		try {
			logger.info("VO => MAP");
			Class clazz = vo.getClass();
			Field[] fields = clazz.getDeclaredFields();
			
			for (Field field : fields) {
				String fieldname = field.getName();
				 
				PropertyDescriptor pd = new PropertyDescriptor(fieldname, clazz);
				Method getMethod = pd.getReadMethod();
				String fieldVal = (String) getMethod.invoke(vo);
				logger.info("TRANSFORM-------" + fieldname + "------" + fieldVal);
				paramap.put(fieldname, fieldVal);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramap;

	}

}
